package com.wp.file.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wp.file.common.BaseContext;
import com.wp.file.dao.FileMapper;
import com.wp.file.pojo.File;
import com.wp.file.pojo.Warehouse;
import com.wp.file.service.FileService;
import com.wp.file.service.WarehouseService;
import com.wp.file.utils.FileSort;
import com.wp.file.utils.FileUtil;
import com.wp.file.utils.FtpHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.time.LocalDate;
@Slf4j
@Service
public class FileServiceImpl extends ServiceImpl<FileMapper, File> implements FileService {

    @Autowired
    private WarehouseService warehouseService;

    /**
     * 判断该文件是否已存储
     * @param md5
     * @return
     */
    public File md5IsExist(String md5) {
        LambdaQueryWrapper<File> lqw = new LambdaQueryWrapper<>();
        lqw.eq(File::getMark,md5);
        File oneFile = super.getOne(lqw);
        //查到已经存在时
        return oneFile;
    }


    /**
     * 保存并上传文件
     */
    @Override
    @Transactional
    public boolean saveFile(MultipartFile file, String path, String fileMd5) {
        File onefile = md5IsExist(fileMd5);
        //如果该文件没上传过
        if(onefile == null){
            log.info(file.getOriginalFilename()+" 服务器未保存过该文件,正在开始上传~");
            String newName = FileUtil.generateName(file.getOriginalFilename());
            boolean uploadFlag = FtpHelper.uploadFile(file, newName);
            if(!uploadFlag){ //上传失败时
                log.info(file.getOriginalFilename()+" Upload failure");
                return false;
            }
            log.info(file.getOriginalFilename()+" Upload successfully");
            onefile = new File();
            //上传成功则存入数据库
            onefile.setStorename(newName);
            onefile.setPath("/" + LocalDate.now());
            onefile.setSize(file.getSize());
            onefile.setMark(fileMd5);//暂时没定标识
            super.save(onefile);
        }
        //存入用户仓库表
        Warehouse warehouse = new Warehouse();
        warehouse.setUserId(BaseContext.getCurrentId());
        warehouse.setSort(FileSort.getFileType(file.getOriginalFilename()));
        warehouse.setDisplayName(file.getOriginalFilename());
        warehouse.setPath(path);
        warehouse.setFileId(onefile.getId());
        warehouse.setDisplaySize(BigDecimal.valueOf(file.getSize())
                .divide(BigDecimal.valueOf(1.0 * 1024 * 1024), 3, BigDecimal.ROUND_HALF_UP).doubleValue());
        log.info(String.valueOf(BigDecimal.valueOf(file.getSize())
                .divide(BigDecimal.valueOf(1.0 * 1024 * 1024), 3, BigDecimal.ROUND_HALF_UP).doubleValue()));
        log.info(warehouse.getDisplaySize().toString());
        warehouseService.save(warehouse);
        return true;
    }
}
